<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends MY_Admin {

    protected $data;

    function __construct () {
        parent::__construct();

        $this->load->model('category_model');
        $this->load->library('MY_FormParam');
    }

    function index(){
        $objFormParam = $this->my_formparam;
        $this->initParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        
        $mode = $this->input->post('mode');
        switch ($mode){
            case "delete":
                $category_id = $this->input->post('category_id');
                if(!$this->delete('category',array('category_id' => $category_id))) {
                    $this->data["msg"] = "削除が失敗しました。";
                } else {
                    $this->data["msg"] = "削除が完了しました。";
                }
            case "search":
            default:
                $params = array();
                $pager = array(
                        "cur_page"    => $this->input->post('search_pageno'),
                        "total_rows"  => count($this->category_model->getCategoryList()),
                );
                $this->setPager($pager);
                $per_page = $this->pager['per_page'];
                $offset   = $this->offset();

                $this->data['category']     = $this->category_model->getCategoryList($params, true, $per_page, $offset);
                $this->data['category_cnt'] = $this->pager['total_rows'];
                $this->data['pager']        = $this->getPagerTpl();
                break;
        }
        $this->view('admin/category_list', $this->data);
    }

    function edit() {
        $objFormParam = $this->my_formparam;
        $this->initParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        $this->data['arrForm']   = $objFormParam->getFormParamList();
        
        $category_id = $this->input->post('category_id');
        
        $mode = $this->input->post('mode');
        switch ($mode)
        {
            case "pre_edit":
                $arrData = $this->category_model->getCategoryById($category_id);
                $objFormParam->setParam($arrData);

                $this->data['arrForm']     = $objFormParam->getFormParamList();
                $this->data["category_id"] = $category_id;

                $template = 'admin/category_edit';
                break;
            case "complete":
                $validate = $this->category_model->exec_validation('category_edit');
                if($validate['status'] == FALSE){
                    $this->data['arrErr']  = $validate['data'];
                    return $this->view('admin/category_edit',$this->data);
                }

                $params = array();
                $params['category_name'] = $this->input->post('category_name');
                
                if(is_numeric($category_id)){
                    if(!$this->update('category', $params, array('category_id' => $category_id))) {
                        $this->data["msg"] = "登録が失敗しました。";
                        return $this->view('admin/category_edit',$this->data);
                    }
                } else {
                    if(!$this->insert('category',$params)) {
                        $this->data["msg"] = "登録が失敗しました。";
                        return $this->view('admin/category_edit',$this->data);
                    }
                }
                $this->data["msg"] = "登録が完了致しました。";
                $template          = 'admin/category_complete';
                break;
            default:
                $template = 'admin/category_edit';
                break;
        }
        $this->view($template, $this->data);
    }

    public function initParam(&$objFormParam)
    {
        $objFormParam->addParam('カテゴリID', 'category_id');
        $objFormParam->addParam('カテゴリ名', 'category_name');
        $objFormParam->addParam('PageMax', 'search_page_max');
        $objFormParam->addParam('PageNo', 'search_pageno');
    }
}